﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using System.Web;

namespace Portal.CDSP
{
    public class FSiteMapWP : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.FSiteMapWP";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string Title
        {
            get
            {
                return GetText("Title", "CDSP-Sơ đồ trang");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("Defaul",GetText("Defaul","Kiểu ngầm định"),1),                   
                };
            }
        }


        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "FSiteMapWP";
            }
        }


        private string ClientRenderWebPartDefaul(string TabModuleId, string HasPorlet)
        {
            try
            {
                TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
                if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
                //DataTable dtItems = new FSpecialSubjectBLL().ReadingMenuItems(TabModuleCls.Id, TabModuleCls.belongSite);
                DataTable dtItems = new FSpecialSubjectBLL().ReadingMenuItemsForSiteMap(TabModuleCls.belongSite);
                string GroupNewsItemId = string.Empty;
                string GroupNewsItemName = string.Empty;
                int IsGroupNews = 0;
                string Url = string.Empty;
                string Html = Environments.GetExtraStyleSheet(TabModuleCls) +
                "<link rel=stylesheet href=sitemap_files/jquery.treeview.css />" +
                "<script type=text/javascript src=sitemap_files/jquery.min.js></script>" +
                "<script type=text/javascript src=sitemap_files/jquery.treeview.js ></script>" +
                "<script type=text/javascript src=sitemap_files/demo.js></script>" +

                 "<div id=\"leftsub\">" +
                        "<div class=\"leftsubHeader\">" +
                        "<h3>Sơ đồ trang web</h3>" +
                        "<div class=\"clrfix\"></div>" +
                        "</div>" +                    
                "  <div class=\"right_1\">" +
                "    <div style=\"background:url(sitemap_files/bg_sm.jpg) right bottom no-repeat; min-height:250px;\">" +
                "	    <!--site map-->" +
                "	    <ul id=\"navigation\" class=\"treeview\">" +
                "		    <ul>";
                for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
                {
                    GroupNewsItemId = (string)dtItems.Rows[iIndex]["GroupNewsItemId"];
                    GroupNewsItemName = (string)dtItems.Rows[iIndex][Environments.ActiveLanguage + "GroupNewsItemName"];
                    IsGroupNews = (int)dtItems.Rows[iIndex]["IsGroupNews"];
                    if (dtItems.Rows[iIndex]["JumpUrl"].ToString().Trim().Length > 0)
                    {
                        Url = (string)dtItems.Rows[iIndex]["JumpUrl"];
                    }
                    else if (IsGroupNews == 1)
                    {
                        Url = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, GroupNewsItemId, GroupNewsItemName, "_RunTime");
                    }
                    else
                    {
                        Url = "#";
                    }
                    Html += "<li class=\"open\"><a href=\"" + Url + "\">" + GroupNewsItemName + "</a>" +
                            "<ul>";
                    DataTable dtSubItems = new FSpecialSubjectBLL().ReadingSubMenuItems(GroupNewsItemId);
                    if (dtSubItems.Rows.Count > 0)
                    {
                        for (int jIndex = 0; jIndex < dtSubItems.Rows.Count; jIndex++)
                        {
                            string SubGroupNewsItemId = (string)dtSubItems.Rows[jIndex]["GroupNewsItemId"];
                            string SubGroupNewsItemName = string.Empty;
                            try
                            {
                                SubGroupNewsItemName = (string)dtSubItems.Rows[jIndex][Environments.ActiveLanguage + "GroupNewsItemName"];
                            }
                            catch { SubGroupNewsItemName = ""; }
                            string SubJumpUrl = string.Empty;
                            try
                            {
                                SubJumpUrl = (string)dtSubItems.Rows[jIndex]["JumpUrl"];
                            }
                            catch { SubJumpUrl = ""; }
                            string SubUrl = "";
                            DataTable dtHtml = new FSpecialSubjectBLL().ReadingHtmlMenu(SubGroupNewsItemId);
                            if (dtHtml.Rows.Count > 0)
                            {
                                string SubjectHtml = (string)dtHtml.Rows[0]["GroupNewsItemShortContent"];
                                string BodyHtml = (string)dtHtml.Rows[0]["Htmlcontent"];
                                SubUrl = Environments.GetStaticHtmlUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName);
                            }
                            else
                            {
                                if (SubJumpUrl == "")
                                {
                                    SubUrl = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName, "_runtime");
                                }
                                else
                                {
                                    SubUrl = SubJumpUrl;
                                }
                            }
                            Html += "<li><img src=sitemap_files/sitemap.gif> " +
                                    "<a href=\"" + SubUrl + "\">" + SubGroupNewsItemName + "</a>" +
                                    "</li>";
                        }
                    }
                    Html += "       </ul></li>";
                }
                Html += "		</ul>" +
                        "	    <!--site map-->	" +
                        "    </div>" +
                        "</div></div>" +
                        "<div class=\"clear\"></div>";
                return Html;
            }
            catch (Exception ex)
            {
                return "ERROR:" + ex.Message;
            }
        }


        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            try
            {
                if (ViewName.Equals("Defaul"))
                {
                    return ClientRenderWebPartDefaul(TabModuleId, HasPorlet);
                }

                return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }


        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            return new FSpecialSubjectBLL().ReadingForService(belongSite, ActiveLanguage);
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(FSiteMapWP), ActivePage);
        }
    }
}